<%@ page import="njust.course.CourseDAO" %>
<%@ page import="njust.course.Course" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="njust.utils.DBUtil" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.List" %>
<%@ page import="njust.page.Page" %><%--
  Created by IntelliJ IDEA.
  User: 25174
  Date: 2022/4/13
  Time: 15:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script>
        function add(){
            window.location.href="addCourse.jsp";
        }
        function del(){
            window.location.href="delCourse.jsp";
        }
        function hit(){
            window.location.href="hitCourse.jsp";
        }
        function submitForm(name){
            var form=document.getElementById(name);
            // alert('提交表单的id为'+name);
            form.submit();
        }
    </script>
    <style>
        #prepagea{
            width: 180px;
            height: 200px;
            position: absolute;
            left: 10px;
            top: 300px;
        }
        #nextPagea{
            position: absolute;
            width: 180px;
            height: 200px;
            left: 100px;
            top: 300px;
        }

    </style>
</head>
<body>

<%
    List<Page> pages = Page.createPagesByList("course", 3);
    //设置session的另Attribute，若不为空则表明点击了排序功能
    //待实现代码......
    if (request.getAttribute("orderPage")!=null){
        int flag= (int) request.getAttribute("orderPage");
        if (flag==0){
            pages=Page.orderPage("course",3,"desc");
        }else if (flag==1){
            pages=Page.orderPage("course",3,"");
        }
    }
%>
<form action="CourseController">
<table border="1" width="600" id = "courseT">
    <tr>

        <td align = "center"></td>
        <td align = "center">序号</td>
        <td align = "center"><a href="javascript:void(0)">选课人数</a></td>
        <td align = "center">
            <a href="javascript:void(0)" onclick="submitForm('changeOrder')">选课人数</a>
        </td>
        <td align = "center">课程性质</td>
    </tr>
    <%
        int pageNum=0;
        if (session.getAttribute("pageNum")!=null)
        {
            pageNum=(int) session.getAttribute("pageNum");
            System.out.println("当前页面为"+pageNum);
        }
        Page page1=pages.get(pageNum);
        session.setAttribute("pageNum",pageNum);
        List<Map<String, Object>> courses = page1.getList();
        for(Map<String, Object> course:courses){
    %>
    <tr>
        <td align = "center"><input name="courseId" type="checkbox" value=<%=course.get("cId") +""%> ></td>
        <td align = "center"><%=course.get("cId")%></td>
        <td align = "center"><%=course.get("cName") %></td>
        <td align = "center"><%=course.get("cNum") %></td>
        <td align = "center"><%=course.get("cType") %></td>
    </tr>
<%
    }
%>
    <%--    默认提交到CourseController处    --%>
    <input type="hidden" name="courseFormCheck" value="chooseCourse">
</table>
<br>
<input type="button" name="addCourseButton" value="新增" onclick="add()">
<input type="button" name="delCourseButton" value="删除" onclick="del()">
<input type="submit" name="hitCourseButton" value="选课">
</form>

<form action="CourseController" id="prePage" >
    <input type="hidden" name="courseFormCheck" value="prePage">
    <div id="prepagea">
    <a href="javascript:void(0)" onclick="submitForm('prePage')">上一页</a>
    </div>
</form>

<form action="CourseController" id="nextPage">
    <div id="nextPagea" >
    <a href="javascript:void(0)"  onclick="submitForm('nextPage') ">下一页</a>
    </div>
    <input type="hidden" name="courseFormCheck" value="nextPage">
</form>

<%--排序部分表单--%>
<form id="changeOrder" action="CourseController">
    <input type="hidden" name="courseFormCheck" value="changeOrder">
</form>

<%
    int size=pages.size();
    for (int i=1;i<=size;i++){
        String changePage="changePage"+i;
%>
<form action="CourseController" id=<%=changePage%> >
    <input type="hidden" name="courseFormCheck" value=<%=changePage%>>
</form>
<%
    }
%>
<%--数字页码跳转超链接--%>
<%
    for (int i=1;i<=size;i++){
%>

    <a href="javascript:void(0)" onclick="submitForm('changePage'+<%=i%>)">
            <%=i%>
    </a>

<%
    }
%>
</body>
</html>
